// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 2008-7-11 11:10:10
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   InitHibernatePlugin.java

package com.creawor.imei.base;

import java.io.PrintStream;
import javax.naming.*;
import javax.servlet.ServletException;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

// Referenced classes of package com.creawor.imei.base:
//            DBUtil

public class InitHibernatePlugin
    implements PlugIn
{

    public InitHibernatePlugin()
    {
    }

    public void destroy()
    {
        if(ctx != null)
            try
            {
                ctx.unbind("HibernateSessionFactory");
            }
            catch(NamingException e)
            {
                e.printStackTrace();
            }
        if(sessionFactory != null)
        {
            try
            {
                sessionFactory.close();
            }
            catch(HibernateException e)
            {
                e.printStackTrace();
            }
            sessionFactory = null;
        }
        DBUtil.destroy();
    }

    public void init(ActionServlet servlet, ModuleConfig config)
        throws ServletException
    {
        try
        {
            sessionFactory = (new Configuration()).configure(getClass().getClassLoader().getResource("hibernate.cfg.xml")).buildSessionFactory();
            //System.out.println("\n\n******* InitHibernatePlugin Starting ****** ");
        }
        catch(HibernateException ex)
        {
            throw new RuntimeException((new StringBuilder("Exception building SessionFactory: ")).append(ex.getMessage()).toString());
        }
        try
        {
            ctx = new InitialContext();
            ctx.bind("HibernateSessionFactory", sessionFactory);
        }
        catch(NamingException ex)
        {
            throw new RuntimeException((new StringBuilder("Exception binding SessionFactory to JNDI: ")).append(ex.getMessage()).toString());
        }
    }

    private Context ctx;
    private SessionFactory sessionFactory;
}